<?php

/**
 * Release form base class.
 *
 * @package    form
 * @subpackage release
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 8807 2008-05-06 14:12:28Z fabien $
 */
class BaseReleaseForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                    => new sfWidgetFormInputHidden(),
      'nombre'                => new sfWidgetFormInput(),
      'video_id'              => new sfWidgetFormPropelSelect(array('model' => 'Video', 'add_empty' => false)),
      'subtitulos_coloreados' => new sfWidgetFormInput(),
      'codec_video_id'        => new sfWidgetFormPropelSelect(array('model' => 'CodecVideo', 'add_empty' => true)),
      'codec_audio_id'        => new sfWidgetFormPropelSelect(array('model' => 'CodecAudio', 'add_empty' => true)),
      'contenedor_id'         => new sfWidgetFormPropelSelect(array('model' => 'Contenedor', 'add_empty' => true)),
      'resolucion'            => new sfWidgetFormInput(),
      'fuente_video_id'       => new sfWidgetFormPropelSelect(array('model' => 'FuenteVideo', 'add_empty' => true)),
      'censurado'             => new sfWidgetFormInput(),
      'recomendado'           => new sfWidgetFormInput(),
      'insertado'             => new sfWidgetFormDate(),
      'finalizado'            => new sfWidgetFormInput(),
      'idioma_x_release_list' => new sfWidgetFormPropelSelectMany(array('model' => 'Idioma')),
      'release_x_fansub_list' => new sfWidgetFormPropelSelectMany(array('model' => 'Fansub')),
    ));

    $this->setValidators(array(
      'id'                    => new sfValidatorPropelChoice(array('model' => 'Release', 'column' => 'id', 'required' => false)),
      'nombre'                => new sfValidatorString(array('max_length' => 50)),
      'video_id'              => new sfValidatorPropelChoice(array('model' => 'Video', 'column' => 'id')),
      'subtitulos_coloreados' => new sfValidatorInteger(array('required' => false)),
      'codec_video_id'        => new sfValidatorPropelChoice(array('model' => 'CodecVideo', 'column' => 'id', 'required' => false)),
      'codec_audio_id'        => new sfValidatorPropelChoice(array('model' => 'CodecAudio', 'column' => 'id', 'required' => false)),
      'contenedor_id'         => new sfValidatorPropelChoice(array('model' => 'Contenedor', 'column' => 'id', 'required' => false)),
      'resolucion'            => new sfValidatorString(array('max_length' => 10, 'required' => false)),
      'fuente_video_id'       => new sfValidatorPropelChoice(array('model' => 'FuenteVideo', 'column' => 'id', 'required' => false)),
      'censurado'             => new sfValidatorInteger(array('required' => false)),
      'recomendado'           => new sfValidatorInteger(array('required' => false)),
      'insertado'             => new sfValidatorDate(),
      'finalizado'            => new sfValidatorInteger(),
      'idioma_x_release_list' => new sfValidatorPropelChoiceMany(array('model' => 'Idioma', 'required' => false)),
      'release_x_fansub_list' => new sfValidatorPropelChoiceMany(array('model' => 'Fansub', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('release[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Release';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['idioma_x_release_list']))
    {
      $values = array();
      foreach ($this->object->getIdiomaXReleases() as $obj)
      {
        $values[] = $obj->getIdiomaId();
      }

      $this->setDefault('idioma_x_release_list', $values);
    }

    if (isset($this->widgetSchema['release_x_fansub_list']))
    {
      $values = array();
      foreach ($this->object->getReleaseXFansubs() as $obj)
      {
        $values[] = $obj->getFansubId();
      }

      $this->setDefault('release_x_fansub_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveIdiomaXReleaseList($con);
    $this->saveReleaseXFansubList($con);
  }

  public function saveIdiomaXReleaseList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['idioma_x_release_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(IdiomaXReleasePeer::RELEASE_ID, $this->object->getPrimaryKey());
    IdiomaXReleasePeer::doDelete($c, $con);

    $values = $this->getValue('idioma_x_release_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new IdiomaXRelease();
        $obj->setReleaseId($this->object->getPrimaryKey());
        $obj->setIdiomaId($value);
        $obj->save();
      }
    }
  }

  public function saveReleaseXFansubList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['release_x_fansub_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(ReleaseXFansubPeer::RELEASE_ID, $this->object->getPrimaryKey());
    ReleaseXFansubPeer::doDelete($c, $con);

    $values = $this->getValue('release_x_fansub_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new ReleaseXFansub();
        $obj->setReleaseId($this->object->getPrimaryKey());
        $obj->setFansubId($value);
        $obj->save();
      }
    }
  }

}
